package com.zimmur.app.mocolink.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zimmur.app.mocolink.dao.GroupGoodsMapper;
import com.zimmur.app.mocolink.dao.OrderDetailMapper;
import com.zimmur.app.mocolink.dao.UserCommentGoodsMapper;
import com.zimmur.app.mocolink.dao.UserCommentGroupMapper;
import com.zimmur.app.mocolink.domain.common.Constant;
import com.zimmur.app.mocolink.domain.model.GroupGoods;
import com.zimmur.app.mocolink.domain.model.Order;
import com.zimmur.app.mocolink.domain.model.OrderDetail;
import com.zimmur.app.mocolink.domain.model.UserCommentGoods;
import com.zimmur.app.mocolink.domain.model.UserCommentGroup;
import com.zimmur.app.mocolink.domain.util.DateUtils;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.vo.UserCommentGoodsInfo;
import com.zimmur.app.mocolink.domain.vo.UserCommentGroupInfo;
import com.zimmur.app.mocolink.domain.vo.UserCommentInfo;
import com.zimmur.app.mocolink.service.IOrderService;
import com.zimmur.app.mocolink.service.IUserCommentService;

@Service
public class UserCommentServiceImpl implements IUserCommentService{

	@Autowired
	private UserCommentGoodsMapper commentGoodsMapper;
	@Autowired
	private UserCommentGroupMapper commentGroupMapper;
	@Autowired
	private IOrderService orderService;
	@Autowired
	private GroupGoodsMapper groupGoodsMapper;
	@Override
	public PageUtil<UserCommentGoodsInfo> queryGoodsCommetntByUserId(Integer userId, Integer pageNo) {
		PageUtil<UserCommentGoodsInfo> page = new PageUtil<>();
		page.setPageNo(pageNo);
		List<UserCommentGoodsInfo> list = commentGoodsMapper.selectInfoByUserId(userId, page.getPageBeginNo(), page.getPageSize());
		int count = commentGoodsMapper.countInfoByUserId(userId);
		page.setItemList(list);
		page.setTotalRecord(count);
		return page;
	}

	@Override
	public PageUtil<UserCommentGroupInfo> queryGroupCommentByUserId(Integer userId, Integer pageNo) {
		PageUtil<UserCommentGroupInfo> page = new PageUtil<>();
		page.setPageNo(pageNo);
		List<UserCommentGroupInfo> list = commentGroupMapper.selectInfoByUserId(userId, page.getPageBeginNo(), page.getPageSize());
		int count = commentGroupMapper.countInfoByUserId(userId);
		page.setItemList(list);
		page.setTotalRecord(count);
		return page;
	}

	@Override
	public int addGoodsComment(Integer userId, String content, Integer orderDetailId) {
		OrderDetail orderDetail = orderService.selectDetailById(orderDetailId);
		if(orderDetail != null && orderDetail.getIsComment() == 0){
			Order order = orderService.selectByOrderId(orderDetail.getOrderId());
			if(order == null || !Constant.Order_Status_Wait_Comment.equals(order.getStatus())){
				return -2;
			}
			UserCommentGoods commentGoods = new UserCommentGoods();
			commentGoods.setContent(content);
			commentGoods.setCoverImage(orderDetail.getCoverImg());
			commentGoods.setCreateTime(DateUtils.getNow());
			commentGoods.setGoodsColor(orderDetail.getColorName());
			commentGoods.setGoodsId(orderDetail.getGoodsId());
			commentGoods.setGoodsName(orderDetail.getGoodsName());
			commentGoods.setGoodsSize(orderDetail.getSizeName());
			commentGoods.setIsShow((byte)1);
			commentGoods.setUserId(userId);
			int flag = commentGoodsMapper.insertSelective(commentGoods);
			if(flag>0){
				orderDetail.setIsComment((byte)1);
				orderService.commentOrderDetail(orderDetail);
			}
			return flag;
		}else{
			return -1;
		}
	}

	@Override
	public int addGroupComment(Integer userId, String content, Integer groupGoodsId) {
		GroupGoods groupGoods = groupGoodsMapper.selectByPrimaryKey(groupGoodsId);
		if(groupGoods!=null){
			UserCommentGroup commentGroup = new UserCommentGroup();
			commentGroup.setContent(content);
			commentGroup.setCoverImage(groupGoods.getCoverImage());
			commentGroup.setCreateTime(DateUtils.getNow());
			commentGroup.setGroupGoodsId(groupGoodsId);
			String desc = groupGoods.getDescription();
			if(desc != null && desc.length()>64){
				desc = desc.substring(0, 64);
			}
			commentGroup.setDescription(desc);
			commentGroup.setIsShow((byte)1);
			commentGroup.setUserId(userId);
			int num = groupGoods.getCommentQuantity()==null?0:groupGoods.getCommentQuantity();
			num++;
			groupGoods.setCommentQuantity(num);
			groupGoodsMapper.updateByPrimaryKeySelective(groupGoods);
			int flag = commentGroupMapper.insertSelective(commentGroup);
			return flag;
		}
		return -1;
	}

	@Override
	public PageUtil<UserCommentInfo> selectByGoodsId(Integer goodsId, Integer pageNo) {
		PageUtil<UserCommentInfo> page = new PageUtil<>();
		page.setPageNo(pageNo);
		List<UserCommentInfo> list = commentGoodsMapper.selectInfoByGoodsId(goodsId, page.getPageBeginNo(), page.getPageSize());
		int count = commentGoodsMapper.countInfoByGoodsId(goodsId);
		page.setItemList(list);
		page.setTotalRecord(count);
		return page;
	}

	@Override
	public PageUtil<UserCommentInfo> selectByGroupGoodsId(Integer groupGoodsId, Integer pageNo) {
		PageUtil<UserCommentInfo> page = new PageUtil<>();
		page.setPageNo(pageNo);
		List<UserCommentInfo> list = commentGroupMapper.selectInfoByGroupId(groupGoodsId, page.getPageBeginNo(), page.getPageSize());
		int count = commentGroupMapper.countInfoByGroupId(groupGoodsId);
		page.setItemList(list);
		page.setTotalRecord(count);
		return page;
	}

}
